const dateTimePicker = require('../../../../utils/dateTimePicker.js');
const app = getApp();
Page({

  data: {
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2018,
    endYear: 2050,
    date: null,
    time: '12:00',
    Ivalue:1,
    color: '',
    index:0,
    price: '',
    total:0,
     sex:[
      { color: '#60CD5F', text: '先生' }, { color: '#DEDEDE', text: '女士' }
    ],
    name:'',
    quota:'',
    id:'',
    numPrice: '1',//1，全款；2，定金
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let name=options.name,
        id=options.id,
        price = options.price;
    let self=this,
        value = wx.getStorageSync('userInfo');
        if (!value) {
          wx.switchTab({
            url: '../../../my/my',
          })
        }
    let obj1=dateTimePicker.dateTimePicker(2018, 2050, this.data.data);
    self.setData({
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      id: id,
      // quota: quota,
      name: name,
      price: Number(price),
      total: price
    })

  },

 
  onReady: function () {

  },
  sex(e){
    let index=e.target.dataset.index,
        sex=this.data.sex;    
    for (var i = 0; i < sex.length;i++)
    {
      sex[i].color ="#DEDEDE";
      sex[index].color ="#60CD5F";
      this.setData({ sex:sex , index:index})      
    }
  },
  reduce(){
    let self = this,
      Ivalue = self.data.Ivalue, //人数 
      num = 1,
      price = self.data.price;
    if (Ivalue <= 1) {
      self.setData({ Ivalue: 1, total: (num * price).toFixed(2)})
    } else {
      self.setData({ Ivalue: Ivalue - 1, total: ((Ivalue - 1) * price).toFixed(2)})
    }
    
  },
  add(){
    let self=this,
        Ivalue = self.data.Ivalue, //人数 
        num=3,
        price=self.data.price;
    if (Ivalue>=3)
      {
      self.setData({ Ivalue: 3, total: (num * price).toFixed(2)})
      }else{
      self.setData({ Ivalue: Ivalue + 1, total: ((Ivalue+1) * price).toFixed(2)})   
      }
  },
  blur1(e){
    let value=e.detail.value,
        price = this.data.price;  
    if(value<=3 && value>0)
    {
      this.setData({ Ivalue: value, total: (price * value).toFixed(2)})
    }else{
      this.setData({ Ivalue: 1, total: (price * 1).toFixed(2)})
    }
  },
  
  changeDateTimeColumn1(e){
    let self=this;
    let arr = self.data.dateTime1,
    dateArr = self.data.dateTimeArray1;
    arr[e.detail.column]=e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    self.setData({ dateTimeArray1: dateArr, dateTime1: arr})
  },
  formSubmit(e)
  {
    let number=e.detail.value.number,  //数量
            _this = this,
            id =this.data.id,
            token=app.globalData.userInfo.token,
        phone=e.detail.value.phone,  //手机号码
      nicheng = e.detail.value.niCheng, //昵称
        BZ=e.detail.value.BZ,    //备注
        date = this.data.dateTime1, 
        index= this.data.index,  //索引
          sex=this.data.sex[index].text,  //先生/女士
        total= this.data.total,  //总价
        numPrice = this.data.numPrice,
        time = this.data.dateTimeArray1[0][date[0]] + "-" + this.data.dateTimeArray1[1][date[1]] + "-" + this.data.dateTimeArray1[2][date[2]] + " " + this.data.dateTimeArray1[3][date[3]] + ":" + this.data.dateTimeArray1[4][date[4]];   //时间
    console.log(this.data,'id')
        if(phone=="" || phone==null)
        {
          wx.showToast({
            title: '手机号不为空',
          })
          return;
        }else{
          var mobile = /^[1][3,4,5,7,8][0-9]{9}$/;
          if (!mobile.test(phone))
          {
            wx.showToast({
              title: '手机号码错误',
            })
            return;
          }
          if (nicheng == "" || nicheng == null) {
            wx.showToast({
              title: '昵称不为空',
            })
            return;
          }
          app.util.ajax({
            url: "api/Order/addOrder",
            method: 'POST',
            data: {
              service_id: id,
              number: number,
              book_time: time,
              phone: phone,
              name: nicheng,
              sex: sex,
              note: BZ,
              token: token,
              order_type: numPrice,
            }
          }, function (res) {
            
            let order_id=res.data.data.order_id,
                order_sn = res.data.data.order_sn,
                order_amount = res.data.data.order_amount;
            app.util.pageJump("./paymentPassword/paymentPassword?order_id=" + order_id + '&order_sn=' + order_sn + '&price=' +order_amount);
          })
       
        }
  }
})